<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->
    <van-cell title="头像" is-link>
      <van-image
        class="avatar"
        fit="cover"
        round
        :src="user.photo"
        @click="$refs.inputFile.click()"
      />
    </van-cell>

    <input type="file" hidden ref="inputFile" @change="inputChange" />

    <van-cell
      title="昵称"
      :value="user.name"
      is-link
      @click="isShowUpdateName = true"
    />
    <van-cell
      title="性别"
      :value="user.gender === 1 ? '女' : '男'"
      is-link
      @click="isShowUpdateGender = true"
    />
    <van-cell
      title="生日"
      :value="user.birthday"
      is-link
      @click="isShowUpdateBirthdy = true"
    />

    <!-- 编辑昵称弹层 -->
    <van-popup
      v-model="isShowUpdateName"
      style="height: 100%"
      position="bottom"
      id="isShowUpdateName"
      ref="isShowUpdateName"
    >
      <update-name
        id="isShowUpdateNameComp"
        ref="isShowUpdateNameComp"
        v-if="isShowUpdateName"
        @close="isShowUpdateName = false"
        v-model="user.name"
      />
    </van-popup>
    <!-- 编辑昵称弹层 -->

    <!-- 编辑性别弹层 -->
    <van-popup
      v-model="isShowUpdateGender"
      position="bottom"
      style="height: 30%"
    >
      <update-gender
        @close="isShowUpdateGender = false"
        v-if="isShowUpdateGender"
        v-model="user.gender"
      />
    </van-popup>
    <!-- 编辑昵称弹层 -->

    <!-- 编辑生日弹层 -->
    <van-popup v-model="isShowUpdateBirthdy" position="bottom">
      <update-birth
        v-if="isShowUpdateBirthdy"
        v-model="user.birthday"
        @close="isShowUpdateBirthdy = false"
      />
    </van-popup>
    <!-- 编辑生日弹层 -->

    <!-- 用户头像编辑 -->
    <van-popup v-model="isShowUpdateAvar" position="bottom" style="height:100%">
      <update-avatar
        v-if="isShowUpdateAvar"
        @close="isShowUpdateAvar = false"
        :img="imgUrl"
        @update-avatar="user.photo = $event"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUserProfileAPI } from '@/api'
import updateName from './components/update-name.vue'
import updateGender from './components/update-gender.vue'
import updateBirth from './components/update-birth.vue'
import updateAvatar from './components/update-avatar.vue'
export default {
  name: 'updateProfile',
  components: { updateName, updateGender, updateBirth, updateAvatar },
  data () {
    return {
      user: {},
      isShowUpdateName: false,
      isShowUpdateGender: false,
      isShowUpdateBirthdy: false,
      isShowUpdateAvar: false,
      imgUrl: ''
    }
  },
  created () {
    this.loadProfile()
  },
  methods: {
    async loadProfile () {
      try {
        const res = await getUserProfileAPI()
        this.user = res.data.data
      } catch (err) {
        this.$toast('获取数据失败')
      }
    },
    inputChange () {
      // 获取文件对象
      const file = this.$refs.inputFile.files[0]
      // console.log(file)
      // 方式1：把文件转为blob数据
      this.imgUrl = window.URL.createObjectURL(file)
      // console.log(this.imgUrl)

      // 方式2： 通过h5 fileReader实现
      // const fs = new FileReader()
      // fs.readAsDataURL(file)
      // fs.onload = () => {
      //   this.imgUrl = fs.result
      // }
      this.isShowUpdateAvar = true
      // 每次选择之后清空内容,不然选择同样的图片不会创建对象
      this.$refs.inputFile.value = ''
    }
  }
}
</script>

<style scoped lang="less">
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
}

.van-popup {
  background: #f5f7f9;
}
</style>
